package com.aizuda.oss.configure;

import cn.hutool.core.util.ReflectUtil;
import com.aizuda.oss.domain.OssEnum;
import com.aizuda.oss.properties.OssProperties;
import com.aizuda.oss.strategy.IOssStrategy;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 *
 * @author izyao
 * @since 2022/3/22
 */
@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class OSSAutoConfiguration {

    private final OssProperties properties;

    public OSSAutoConfiguration(OssProperties ossProperties){
        this.properties = ossProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public IOssStrategy ossStrategy(){
        IOssStrategy iOssStrategy = (IOssStrategy) ReflectUtil.newInstance(OssEnum.getStrategyClass(properties.getPrimary()));
        iOssStrategy.init(properties);
        return iOssStrategy;
    }

}
